package com.czl.module_storehouse.activity.stock;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.config.AppConstants;
import com.czl.base.data.bean.tengyun.RoomListBean;
import com.czl.base.util.SpHelper;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_service.event.RoomListEvent;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ActivityStockListBinding;
import com.czl.module_storehouse.fragment.StockDefaultFragment;
import com.czl.module_storehouse.fragment.StockListFragment;
import com.jeremyliao.liveeventbus.LiveEventBus;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.WrapPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_STOCK_LIST)
public class StockListActivity extends BaseActivity<ActivityStockListBinding> {

    private final List<String> mDataList = new ArrayList<>();
    private final List<Fragment> fragments = new ArrayList<>();

    @Override
    protected ActivityStockListBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityStockListBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("库存一览");
        /*int locatId = SpHelper.INSTANCE.decodeInt(Constants.SpKey.STOREHOUSE_LOCATION_ID);
        if (locatId > 0) {
            mDataList.add("默认库位");
            fragments.add(new StockDefaultFragment());
        }*/
        LiveEventBus.get(RoomListEvent.class).observe(this, this::initData);
    }

    private void initData(RoomListEvent event) {
        List<RoomListBean> roomList = event.getRoomList();
        if (roomList == null || roomList.isEmpty()) {
            initMagicIndicator();
            return;
        }
        for (RoomListBean bean : roomList) {
            mDataList.add(bean.getRoomName());
            fragments.add(StockListFragment.getInstance(bean.getRoomId()));
        }
        initMagicIndicator();
    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView fixedPagerTitleView = new SimplePagerTitleView(context);
                fixedPagerTitleView.setText(mDataList.get(index));
                fixedPagerTitleView.setNormalColor(ContextCompat.getColor(getContext(), R.color.color_333333));
                fixedPagerTitleView.setSelectedColor(ContextCompat.getColor(getContext(), R.color.white));
                fixedPagerTitleView.setOnClickListener(v -> binding.viewPager2.setCurrentItem(index));
                return fixedPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                WrapPagerIndicator indicator = new WrapPagerIndicator(context);
                indicator.setFillColor(ContextCompat.getColor(getContext(), R.color.color_25A468));
                return indicator;
            }
        });
        binding.indicator.setNavigator(commonNavigator);
        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.indicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                binding.indicator.onPageScrollStateChanged(state);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                binding.indicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        });
        FragmentAdapter adapter = new FragmentAdapter(this);
        binding.viewPager2.setAdapter(adapter);
    }

    private class FragmentAdapter extends FragmentStateAdapter {

        public FragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }
}
